
#include "fp_device.h"

// 构造函数
fp_Device::fp_Device(const string &devicename, const uint32_t baudRate)
{
    linuxUart = new Dev_Uart(devicename, baudRate);
}

// 析构函数
fp_Device::~fp_Device()
{
    if (linuxUart)
    {
        delete linuxUart;
    }
}

// 发送协议包
bool fp_Device::sendProtocolPacket(vector<uint8_t> packetData)
{
    cout << "发送的数据包:";
    for (auto byte : packetData)
    {
        printf("0x%02X ", byte);
    }
    printf("\n");
    int16_t ret = linuxUart->writeUartData((char *)(packetData.data()), packetData.size());
    if (ret == -1)
    {
        cout << "发送协议包失败" << endl;
        return false;
    }
    return true;
}

// 接收指纹设备的应答包
bool fp_Device::receiveProtocolAckPacket(vector<uint8_t> &packetData)
{
    vector<uint8_t> prefixPacket(ACK_PROTOCOL_FIXED_SIZE, 0);
    bool ret = linuxUart->receiveFixedSizeData((char *)prefixPacket.data(), ACK_PROTOCOL_FIXED_SIZE);
    if (!ret)
    {
        cout << "接收固定长度数据失败" << endl;
        return false;
    }
    uint16_t suffixPacketSize = (prefixPacket[7] << 8) | prefixPacket[8];
    vector<uint8_t> suffixPacket(suffixPacketSize, 0);
    ret = linuxUart->receiveFixedSizeData((char *)suffixPacket.data(), suffixPacketSize);
    if (!ret)
    {
        cout << "接收固定长度数据失败" << endl;
        return false;
    }
    // 合并协议包
    packetData.insert(packetData.end(), prefixPacket.begin(), prefixPacket.end());
    packetData.insert(packetData.end(), suffixPacket.begin(), suffixPacket.end());
    cout << "接收的数据包:";
    for (auto byte : packetData)
    {
        printf("0x%02X ", byte);
    }
    printf("\n");
    return true;
}

// 握手函数
bool fp_Device::handshake()
{
    // 生成握手协议内容
    Fp_protocol protocol = Fp_protocol::HS_Protocol();
    bool ok = sendProtocolPacket((*Fp_protocol::Get_vector()));
    if (!ok)
    {
        cout << "发送握手协议失败" << endl;
        return false;
    }
    vector<uint8_t> packetData;
    ok = receiveProtocolAckPacket(packetData);
    if (!ok)
    {
        cout << "接收握手应答包失败" << endl;
        return false;
    }
    // 生成应答协议
    Fp_protocol ackProtocol = Fp_protocol::Acknow_Protocol(packetData);

    // 校验包
    ok = ackProtocol.isValidPacketData();
    if (!ok)
    {
        cout << "应答包校验失败" << endl;
        return false;
    }
    // 校验操作码
    ok = ackProtocol.verifyOperationResult();
    if (!ok)
    {
        cout << "操作失败" << endl;
        return false;
    }
    return true;
}

// 录指纹图像
bool fp_Device::captureFingerprintImage()
{
    Fp_protocol protocol = Fp_protocol::GetImage_Protocol();
    cout << "发送获取指纹图像协议---->";
    bool ok = sendProtocolPacket((*Fp_protocol::Get_vector()));
    if (!ok)
    {
        cout << "发送获取指纹图像协议失败" << endl;
        return false;
    }
    cout << "接收获取指纹图像协议<----";
    vector<uint8_t> packetData;
    ok = receiveProtocolAckPacket(packetData);
    if (!ok)
    {
        cout << "接收获取指纹图像应答包失败" << endl;
        return false;
    }
    Fp_protocol ackProtocol = Fp_protocol::Acknow_Protocol(packetData);
    ok = ackProtocol.isValidPacketData();
    if (!ok)
    {
        cout << "应答包校验失败" << endl;
        return false;
    }
    ok = ackProtocol.verifyOperationResult();
    if (!ok)
    {
        cout << "操作失败" << endl;
        return false;
    }
    return true;
}

// 生成指纹特征码
bool fp_Device::generate_fp_FeatureCode(uint8_t bufId)
{
    Fp_protocol protocol = Fp_protocol::Feature_Protocol(bufId);
    cout << "发送指纹特征码协议---->";
    bool ok = sendProtocolPacket((*Fp_protocol::Get_vector()));
    if (!ok)
    {
        cout << "发送指纹特征码协议失败" << endl;
        return false;
    }
    cout << "接收指纹特征码协议<----";
    vector<uint8_t> packetData;
    ok = receiveProtocolAckPacket(packetData);
    if (!ok)
    {
        cout << "接收指纹特征码应答包失败" << endl;
        return false;
    }
    Fp_protocol ackProtocol = Fp_protocol::Acknow_Protocol(packetData);
    ok = ackProtocol.isValidPacketData();
    if (!ok)
    {
        cout << "应答包校验失败" << endl;
        return false;
    }
    ok = ackProtocol.verifyOperationResult();
    if (!ok)
    {
        cout << "操作失败" << endl;
        return false;
    }
    return true;
}

// 合成指纹模板
bool fp_Device::synthesize_fp_Template()
{
    Fp_protocol protocol = Fp_protocol::Template_Protocol();
    cout << "发送合成指纹模板协议---->";
    bool ok = sendProtocolPacket((*Fp_protocol::Get_vector()));
    if (!ok)
    {
        cout << "发送合成指纹模板协议失败" << endl;
        return false;
    }
    cout << "接收合成指纹模板协议<----";
    vector<uint8_t> packetData;
    ok = receiveProtocolAckPacket(packetData);
    if (!ok)
    {
        cout << "接收合成指纹模板应答包失败" << endl;
        return false;
    }
    Fp_protocol ackProtocol = Fp_protocol::Acknow_Protocol(packetData);
    ok = ackProtocol.isValidPacketData();
    if (!ok)
    {
        cout << "应答包校验失败" << endl;
        return false;
    }
    ok = ackProtocol.verifyOperationResult();
    if (!ok)
    {
        cout << "操作失败" << endl;
        return false;
    }
    return true;
}

// 保存模板
bool fp_Device::saveTemplate(uint16_t pageId, uint8_t bufId)
{
    Fp_protocol protocol = Fp_protocol::Store_Protocol(pageId, bufId);
    cout << "发送存储指纹模板协议---->";
    bool ok = sendProtocolPacket((*Fp_protocol::Get_vector()));
    if (!ok)
    {
        cout << "发送存储指纹模板协议失败" << endl;
        return false;
    }
    cout << "接收存储指纹模板协议<----";
    vector<uint8_t> packetData;
    ok = receiveProtocolAckPacket(packetData);
    if (!ok)
    {
        cout << "接收存储指纹模板应答包失败" << endl;
        return false;
    }
    Fp_protocol ackProtocol = Fp_protocol::Acknow_Protocol(packetData);
    ok = ackProtocol.isValidPacketData();
    if (!ok)
    {
        cout << "应答包校验失败" << endl;
        return false;
    }
    ok = ackProtocol.verifyOperationResult();
    if (!ok)
    {
        cout << "操作失败" << endl;
        return false;
    }
    return true;
}

// 分布录指纹
bool fp_Device::enrollFingerprint(uint16_t pos)
{
    bool ok = false;
    uint8_t errCount = 0;
    for (int i = 1; i <= 2; i++)
    {
        cout << "第" << i << "次获取指纹图像" << endl;
        for (int j = 1; j <= 5; j++)
        {
            ok = captureFingerprintImage();
            if (ok)
            {
                break;
            }
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
        if (!ok)
        {
            return false;
        }
        cout << "生成指纹特征" << endl;
        ok = generate_fp_FeatureCode(i);
        if (!ok)
        {
            errCount++;
            cout << "生成指纹特征失败" << endl;
            i--;
            if (errCount == 5)
            {
                return false;
            }
        }
        else
        {
            cout << "第" << i << "次生成指纹特征成功" << endl;
            errCount = 0;
        }
    }
    ok = synthesize_fp_Template();
    if (!ok)
    {
        cout << "合成模板失败!" << endl;
        return false;
    }
    ok = saveTemplate(pos);
    if (!ok)
    {
        cout << "保存模板失败!" << endl;
        return false;
    }
    return true;
}

// 读取指纹库中有效模板的个数
bool fp_Device::getVaild_TemplateNums(uint16_t &nums)
{
    cout << "指纹库中有效模板的个数" << endl;
    Fp_protocol protocol = Fp_protocol::Valid_NumsProtocol();
    cout << "获取有效模板个数协议---->" << endl;
    bool ok = sendProtocolPacket((*Fp_protocol::Get_vector()));
    if (!ok)
    {
        cout << "发送获取有效模板个数协议失败" << endl;
        return false;
    }
    cout << "接收获取有效模板个数协议的应答<----" << endl;
    vector<uint8_t> packetData;
    ok = receiveProtocolAckPacket(packetData);
    if (!ok)
    {
        cout << "获取有效模板个数应答包失败" << endl;
        return false;
    }
    Fp_protocol ackProtocol = Fp_protocol::Acknow_Protocol(packetData);
    ok = ackProtocol.isValidPacketData();
    if (!ok)
    {
        cout << "应答包校验失败" << endl;
        return false;
    }
    ok = ackProtocol.verifyOperationResult();
    if (!ok)
    {
        cout << "操作失败" << endl;
        return false;
    }
    // 有效模板个数在payload的第2、3字节
    if (packetData.size() > 11)
    {
        nums = (packetData[10] << 8) | (packetData[11]);
    }
    else
    {
        nums = 0;
    }
    return true;
}

// 清空指纹库
bool fp_Device::clear_fp_Data()
{
    Fp_protocol protocol = Fp_protocol::Erase_FProtocol();
    cout << "发送清空指纹库协议---->";
    bool ok = sendProtocolPacket((*Fp_protocol::Get_vector()));
    if (!ok)
    {
        cout << "发送清空指纹库协议失败" << endl;
        return false;
    }
    cout << "接收清空指纹库协议的应答<----";
    vector<uint8_t> packetData;
    ok = receiveProtocolAckPacket(packetData);
    if (!ok)
    {
        cout << "接收清空指纹库应答包失败" << endl;
        return false;
    }
    Fp_protocol ackProtocol = Fp_protocol::Acknow_Protocol(packetData);
    ok = ackProtocol.isValidPacketData();
    if (!ok)
    {
        cout << "应答包校验失败" << endl;
        return false;
    }
    ok = ackProtocol.verifyOperationResult();
    if (!ok)
    {
        cout << "操作失败" << endl;
        return false;
    }
    return true;
}
